﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhaoxi.MSACommerce.Infrastructure._Common;
using ZhaoXi.MSACommerce.Infrastructure.EnityFrameworkCore;
using ZhaoXi.MSACommerce.Infrastructure.EnityFrameworkCore.interceptor;

namespace ZhaoXi.MSACommerce.UserService.Infrastructure
{
    public static class DependencyInjection
    {
        public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
        {
            ConfigureEfCore(services, configuration);
            
            services.AddInfrastructureCommon();
            
            services.AddInfrastructurea(configuration);//自己修改
            return services;
        }

        private static void ConfigureEfCore(IServiceCollection services, IConfiguration configuration)
        {
            var dbConnection = configuration.GetConnectionString("DefaultConnection");

            services.AddDbContext<UserDbContext>((sp, options) =>
            {
                
                options.AddInterceptors(sp.GetRequiredService<AuditEntityInterceptor>());
                options.UseMySql(dbConnection, ServerVersion.AutoDetect(dbConnection));
            });
        }
    }
}
